<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=0">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>添加客房</title>

    <!-- Bootstrap -->
    <link rel="stylesheet" href="static/bootstrap-3.4.1/css/bootstrap.css">
    <link rel="stylesheet" href="static/bootstrap-3.4.1/css/bootoast.css">
    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="static/bootstrap-3.4.1/js/jquery-3.6.0.min.js"></script>
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="static/bootstrap-3.4.1/js/bootstrap.js"></script>
    <script src="./static/bootstrap-3.4.1/js/bootoast.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        main .myform {
            width: 30%;
            margin-left: 40px;
        }

        main p {
            display: block;
            width: 100%;
            /* position: absolute;
            bottom: 20px; */
            text-align: center;
            margin: 20px 0;
        }

        .footer {
            position: absolute;
            left: 0;
            right: 0;
            bottom: 10px;
            color: #666;
            text-align: center;
        }
    </style>
    <script>
        $(function (){
            initMenu();
        })

        /**
         * 初始化客房下拉菜单
         */
        function initMenu(){
            $.ajax({
                type:'get',
                url:'/SelectCourse2/teacher/queryList',
                cache: false,
                dataType: 'json',
                success:function (res){
                    console.log("请求客房数据：",res.result);
                    if(res.code == 1 && res.result){
                        // 动态生成下拉列表
                        $("#teacherId").append($(`
                              <option className = "disabled" readonly value="">请选择房间</option>
                        `))
                        for(let val of res.result){
                            $("#teacherId").append($(`
                                <option value="${val.userId}">${val.nickname}</option>
                            `))
                        }
                    }
                },
                error:function (err){
                    bootoast({
                        message: '服务器请求错误！',
                        type: 'danger',
                        position: 'top',
                        timeout: 2
                    });
                }
            })
        }

    </script>
</head>

<body>
<div class="container-fluid my-container">
    <main>
        <!-- 路径导航 -->
        <ol class="breadcrumb" style="margin-top:5px;">
            <li><a href="#">主页</a></li>
            <li><a href="#">客房管理</a></li>
            <li class="active">添加客房</li>
        </ol>
        <!-- 表单 -->
        <div class="myform">
            <form id="myForm" action="/SelectCourse2/course/add" method="post">
                <div class="form-group">
                    <label for="courseName">房间号：</label>
                    <input type="text" class="form-control" name="courseName" id="courseName" placeholder="请输入房间号">
                </div>
                <div class="form-group">
                   <label for="courseName">入住人数：</label><br>
                 <input type="text" class="form-control" name="courseid" id="courseId" placeholder="请输入入住人数">
                    
                </div>
                <div class="form-group">
                    <label for="textbook">价<span style="color:transparent;user-select: none;">隐藏</span>格：</label>
                    <input type="text" class="form-control" name="textbook" id="textbook" placeholder="请输入价格">
                </div>
                <div class="form-group">
                    <label for="credit">是否空闲</label><br>
					<input type="radio"  name="credit" id="credit"/>是<span style="color:transparent;user-select: none;">隐藏</span>
					     <input  type="radio" name="credit"/>否</br>
                </div>
				<div class="form-group">
				    <label for="classHours">房<span style="color:transparent;user-select: none;">隐藏</span>型：</label>
				    <input type="number" class="form-control" name="classHours" id="classHours" placeholder="请输入房型">
				</div>
                <button type="button" class="btn btn-success" onclick="save()">保存</button>
            </form>

        </div>
    </main>
</div>
<script>
    // 提交表单的方法
    function save() {
        // 获取表单数据
        let courseName = $("#courseName").val();
        let teacherId = $("#teacherId option:selected").val();
        let textbook = $("#textbook").val();
        let credit = $("#credit").val();
        let classHours = $("#classHours").val();

        console.log(courseName, teacherId, textbook, credit,classHours);
        // 进行非空校验
        if (!courseName || courseName === '') {
            bootoast({
                message: '客房名称不能为空',
                type: 'danger',
                position: 'top',
                timeout: 2
            });
            return false;
        }
        if (!teacherId || teacherId === '') {
            bootoast({
                message: '房间号不能为空',
                type: 'danger',
                position: 'top',
                timeout: 2
            });
            return false;
        }
        if (!textbook || textbook === '') {
            bootoast({
                message: '入住人数不能为空',
                type: 'danger',
                position: 'top',
                timeout: 2
            });
            return false;
        }
        if (!credit || credit === '') {
            bootoast({
                message: '价格不能为空',
                type: 'danger',
                position: 'top',
                timeout: 2
            });
            return false;
        }
        console.log(`courseName=${courseName}&teacherId=${teacherId}&textbook=${textbook}&credit=${credit}&classHours=${classHours}`);
        // 提交表单
        $.ajax({
            type: 'post',// 提交方式 post
            url: $("#myForm").attr("action"),// /SelectCourse2/course/add
            cache: false,// 不启用缓存
            data: `courseName=${courseName}&teacherId=${teacherId}&textbook=${textbook}&credit=${credit}&classHours=${classHours}`,// 数据可以自由拼接
            dataType: 'json',// 期望服务器响应的数据类型 json
            success: function (result) {// 响应成功就会自动执行该方法，result中封装的是响应的结果
                console.log(result);
                // 提示
                bootoast({
                    message: result.msg,
                    type: 'success',
                    position:'top',
                    timeout:2
                });
            },
            error: function (error) {// 响应失败就会自动执行该方法，error中封装的是错误信息
                // 提示
                bootoast({
                    message: '服务器请求错误！',
                    type: 'danger',
                    position:'top',
                    timeout:2
                });
            }

        })
        return true;
    }
</script>
</body>
</html>